package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.MerchantManagement;
import com.ruoyi.recycle.dto.MerchantManagementDTO;
import com.ruoyi.recycle.service.IMerchantManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 综合站管理Controller
 *
 * @author Wubk
 * @date 2022-05-21
 */
@RestController
@RequestMapping("/merchant")
@Api(tags = "综合站管理")
public class MerchantManagementController extends BaseController {
    @Autowired
    private IMerchantManagementService merchantManagementService;

    /**
     * 查询综合站管理列表
     */
    @RequiresPermissions("user:merchant:list")
    @GetMapping("/list")
    public TableDataInfo list(MerchantManagement merchantManagement) {
        startPage();
        List<MerchantManagement> list = merchantManagementService.selectMerchantManagementList(merchantManagement);
        return getDataTable(list);
    }

    /**
     * 分页条件查询
     */
    @PostMapping("/merchantPageQuery")
    @ApiOperation("分页条件查询")
    public IPage<MerchantManagement> merchantPageQuery(@RequestBody MerchantManagementDTO queryDTO) {
        final MerchantManagement merchant = new MerchantManagement();
        BeanUtils.copyProperties(queryDTO, merchant);
        final LambdaQueryWrapper<MerchantManagement> wrapper = Wrappers.lambdaQuery(merchant);
        wrapper.orderByAsc(MerchantManagement::getCreatedTime);
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(MerchantManagement::getCreatedTime, queryDTO.getSearchTimeBegin());
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(MerchantManagement::getCreatedTime, queryDTO.getSearchTimeEnd());
        }
        wrapper.orderByDesc(MerchantManagement::getCreatedTime);
        return merchantManagementService.page(new Page<MerchantManagement>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 添加积分
     */
    @GetMapping("/saveIntegral")
    @ApiOperation(value = "添加积分")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "综合站ID", dataType = "Long"),
            @ApiImplicitParam(name = "integral", value = "积分", dataType = "Long")})
    public AjaxResult saveIntegral(@RequestParam(value = "id",required = true) Long id,@RequestParam(value = "integral",required = true) Long integral) {
        return merchantManagementService.saveIntegral(id,integral);
    }

    /**
     * 获取小程序码
     */
    @GetMapping("/getMiniProgram")
    @ApiOperation("获取小程序码")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getMiniProgram(@RequestParam(value = "id",required = true) Long id) {
        return merchantManagementService.getMiniProgram(id);
    }

    /**
     * 导出综合站管理列表
     */
    @RequiresPermissions("user:merchant:export")
    @ApiOperation("导出综合站管理列表")
    @Log(title = "综合站管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MerchantManagement merchantManagement)
    {
        List<MerchantManagement> list = merchantManagementService.selectMerchantManagementList(merchantManagement);
        ExcelUtil<MerchantManagement> util = new ExcelUtil<MerchantManagement>(MerchantManagement.class);
        util.exportExcel(response, list, "综合站管理数据");
    }

    /**
     * 获取综合站管理详细信息
     */
    @RequiresPermissions("user:merchant:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取综合站管理详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(merchantManagementService.getById(id));
    }

    /**
     * 新增综合站管理
     */
    @RequiresPermissions("user:merchant:add")
    @Log(title = "综合站管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation("新增综合站管理")
    public AjaxResult add(@RequestBody MerchantManagement merchantManagement)
    {
        merchantManagement.setCreatedTime(new Date());
        return toAjax(merchantManagementService.insertMerchantManagement(merchantManagement));
    }

    /**
     * 修改综合站管理
     */
    @RequiresPermissions("user:merchant:edit")
    @Log(title = "综合站管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改综合站管理")
    public AjaxResult edit(@RequestBody MerchantManagement merchantManagement) {
        return toAjax(merchantManagementService.updateMerchantManagement(merchantManagement));
    }

    @ApiOperation("修改综合站管理状态")
    @PostMapping("/changeStatus")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "综合站ID", dataType = "Long"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "String")})
    public AjaxResult changeStatus(@RequestParam Long id, @RequestParam String status) {
        return toAjax(merchantManagementService.changeStatus(id, status));
    }


    /**
     * 删除综合站管理
     */
    @RequiresPermissions("user:merchant:remove")
    @Log(title = "综合站管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除综合站管理")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(merchantManagementService.deleteMerchantManagementByIds(ids));
    }
}
